﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace ScriptManager
{
    class functionFactory
    {
        private static string ApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Logitech\\Logitech Gaming Software\\profiles";
        // 工具类 包含了 数据库 和其他的乱七八糟的
        public string getMD5(string text)
        {
            byte[] sor = Encoding.UTF8.GetBytes(text);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                //加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
                strbul.Append(result[i].ToString("x2"));
            }
            return strbul.ToString();

        }

        /// <summary>  
        /// 获取本机MAC地址  
        /// </summary>  
        /// <returns>本机MAC地址</returns>  
        public  string GetMacAddress()
        {
            try
            {
                string strMac = string.Empty;
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        strMac = mo["MacAddress"].ToString();
                    }
                }
                moc = null;
                mc = null;
                return strMac;
            }
            catch
            {
                return "unknown";
            }
        }
        ///<summary>
        ///字符串转Char字符串
        /// </summary>
        ///<returns>char list</returns>
        public string strToChar(string str)
        {
            List<int> arrList = new List<int>();
            foreach(char s in str)
            {
                arrList.Add((int)s);
            }
            string scriptCore = string.Join(",", arrList.ToArray());
            string head = "loadstring((function()local Dy_9138148 = {";
            string daeh = " }               local Khor = ''    for i = 1,#Dy_9138148    do   Khor = Khor..string.char(Dy_9138148[i])    end    return Khor end)())()";
            string all = head + scriptCore + daeh;
            return all;
            
            // return string.Join(",", arrList.ToArray());

        }

        public void EditProperty(string script, string filePath)
        {
            //实际写入操作
            //读取xml
            XmlDocument doc = new XmlDocument();
            doc.Load($@"{filePath}");
            XmlElement xm = doc.DocumentElement;
            // 读取是不是默认配置文件的选项
            XmlNode profile = xm.FirstChild;
            //获取Script的值
            XmlNode Script = profile.LastChild;
            // 设置Script的值
            Script.InnerText = script;
            doc.Save($@"{filePath}");
            killLcore();
            runLcore();

        }
        public void killLcore()
        {
            Process[] p = Process.GetProcesses();
            foreach (Process p1 in p)
            {
                try
                {
                    string processName = p1.ProcessName.ToLower().Trim();
                    //判断是否包含阻碍更新的进程
                    if (processName == "LCore" || processName == "lightingservice" || processName == "lcore")
                    {
                        p1.Kill();

                    }

                }
                catch { }
            }
        }
        public void runLcore()
        {
            Process pro = new Process();
            string FileName = @"C:\Program Files\Logitech Gaming Software\LCore.exe";
            // 再启动
            if (System.IO.File.Exists(FileName))
            {
                pro.StartInfo.FileName = FileName;
                pro.StartInfo.Arguments = "/min";
                pro.Start();
            }

        }

    }
}
